package kuzmenkov.oip.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This class validates web links such as sites and emails
 * User: Michael Kuzmenkov
 * Date: 28.03.2009
 * Time: 21:44:01
 */
public class LinkValidator {
    /**
     * Validates an email address
     * @param astrEmailId an email address
     * @return true if the email address is valid
     */
    public static boolean validateEmailId(String astrEmailId) {
        if (astrEmailId==null || astrEmailId.isEmpty())
                throw new IllegalArgumentException("astrEmailId is empty: "+astrEmailId);
        //Set the email pattern string
        Pattern p = Pattern.compile(".+@.+\\.[a-zA-Z]+");
        //Match the given string with the pattern
        Matcher m = p.matcher(astrEmailId);
        //check whether match is found
        return m.matches();
    }

    /**
     * Validates a site reference
     * @param address the site references
     * @return true if the reference is valid
     */
    public static boolean validateSiteAddress (String address) {
        if (address==null || address.isEmpty())
                throw new IllegalArgumentException("address is empty: "+address);
        
        String regexp = "((http://)|(www\\.)|(http://www\\.))(.+\\..+)";
        return Pattern.matches(regexp, address);
    }

    
}
